<?php
if ( !defined('ABSPATH') ) {exit;}

$channelMap = array();
$typeMap = array();

function getCode(){
	global $wpdb;
	
	$query = "select * from code_channel_type order by sort asc";
	$channelTypeList = $wpdb->get_results($query);
	foreach ( $channelTypeList as $channelTypeMap ) {
		$type = $channelTypeMap->type;
		$code = $channelTypeMap->code;
		$parent = $channelTypeMap->parent;
		$name = $channelTypeMap->name;
		if($type == 'channel'){
			global $channelMap;
			$channelMap[] = array('type'=>$type, 'code' =>$code , 'parent' => $parent , 'name' => $name);
		}else if($type == 'type'){
			global $typeMap;
			$typeMap[$parent][] = array('type'=>$type, 'code' =>$code , 'parent' => $parent , 'name' => $name);
		}
	}
}

function get_xxurl_info($id){
	global $wpdb;
	$query = "select * from vw_xxurls_links where post_id = '{$id}' ";
	$xxurlsInfo = $wpdb->get_results($query);
	return $xxurlsInfo;
}

function add_script(){
	$script = '<script type="text/javascript">
    var allTypeObj = {
        \'2\':{201: "学前教育",202: "小学教育",203: "初中教育",204: "高中教育",205: "高等教育",206: "论文",207: "研究生考试",208: "行业资料",209: "管理",210: "外语学习",211: "IT计算机",212: "资格/认证考试",213: "其他"}, //教育
        \'1\':{103: "asp源码",104: "php源码",105: ".net源码",106: "jsp源码",107: "其它源码",109: "程序插件",110: "程序模板",111: "书籍教程"},//建站
        \'5\':{501: "平面",502: "多媒体",503: "三维3D"},//素材
        \'10\':{151: "聊天软件",152: "音频软件",153: "视频软件",154: "图像软件",155: "网络软件",156: "系统软件",157: "应用软件",158: "驱动程序",159: "安全软件",160: "教育软件",161: "行业软件",162: "其他软件"},//软件
        \'19\':{180: "mac软件",181: "mac游戏",182: "ipad软件",183: "ipad游戏",184: "iphone软件",185: "iphone游戏",1031: "应用源码",1032: "游戏源码",1033: "代码库"},//苹果
        \'11\':{250: "安卓电视软件",251: "安卓电视游戏",252: "安卓平板软件",253: "安卓平板游戏",254: "安卓手机软件",255: "安卓手机游戏",1028: "应用源码",1029: "游戏源码",1030: "代码库"},//安卓
        \'9\':{901: "单机游戏",902: "网络游戏",903: "电视游戏",904: "手机游戏",905: "游戏攻略",906: "其他游戏"},//游戏
        \'3\':{301: "摄影",302: "海报",303: "画册"},//酷图
        \'12\':{280: "MP3",281: "MV",282: "无损音乐",283: "电影花絮",284: "电视剧花絮",285: "综艺花絮",286: "演唱会"}//影音
    }, onChangeChannel = function(obj){
    	   var value = obj.value;
    	   jQuery("#ordown_xxurl_type").html("<option value=\'0\'>请选择</option>");
    	   if(value == "0"){
    	   	jQuery("#ordown_xxurl_type").val("0");
    	   }else{
    	   	   var typeObj = allTypeObj[value] || {};
		   for(var typeId in typeObj){
    	            if(!typeObj.hasOwnProperty(typeId)) continue;
    	            jQuery("#ordown_xxurl_type").append(\'<option value="\'+typeId+\'">\'+ typeObj[typeId] + "</option>");
    	         }
    	   }
    };
</script>';
	echo $script;
}
add_action( 'admin_menu', 'getCode' );
add_action( 'admin_menu', 'get_xxurl_info' );
add_action( 'admin_menu', 'or_create_xxurl_info_box' );
add_action( 'admin_menu', 'or_create_down_box' );
add_action( 'admin_menu', 'add_script' );
add_action( 'save_post', 'or_save_down_data' );

function or_create_xxurl_info_box() {
	add_meta_box( 'xxurl-info-boxes','XXUrlInfo信息', 'or_xxurl_info', 'post', 'normal', 'high' );
}
function or_xxurl_info_boxes() {  //资源名称、资源大小、更新时间、适用版本、作者信息
	$meta_boxes = array(
		array(
	    "name"             => "post_time",
	    "title"            => "发帖时间",
	    "desc"             => "发帖时间",
	    "type"             => "text",
	    "capability"       => "manage_options"
	    ),
		array(
	    "name"             => "refresh_time",
	    "title"            => "最后刷新时间",
	    "desc"             => "最后刷新时间",
	    "type"             => "text",
	    "capability"       => "manage_options"
	    ),
		array(
	    "name"             => "last_check_status",
	    "title"            => "最后检测状态",
	    "desc"             => "最后检测状态",
	    "type"             => "text",
	    "capability"       => "manage_options"
	    ),
	    array(
	    "name"             => "last_check_time",
	    "title"            => "最后检测时间",
	    "desc"             => "最后检测时间",
	    "type"             => "text",
	    "capability"       => "manage_options"
	    ),
	    array(
	    "name"             => "last_check_url",
	    "title"            => "最后检测页面",
	    "desc"             => "最后检测页面",
	    "type"             => "url",
	    "capability"       => "manage_options"
	    ),
	array(
		"name"             => "link_url",
		"title"            => "XXURL发帖地址",
		"desc"             => "XXURL发帖地址",
		"type"             => "url",
		"capability"       => "manage_options"
	)
	);
	return apply_filters( 'or_xxurl_info_boxes', $meta_boxes );
}
function or_xxurl_info() {
	global $post;
	$meta_boxes = or_xxurl_info_boxes(); 
	$xxurlInfo = get_xxurl_info($post->ID);
?>
	<table class="form-table">
	<?php foreach ( $meta_boxes as $meta ) :
		//$value = array(){ $xxurlInfo[0]->$meta['name'] };
		$name = $meta['name'];
		$value = $xxurlInfo[0]->$name;
		if ( $meta['type'] == 'text' )
			or_show_text_input( $meta, $value);
		elseif ( $meta['type'] == 'textarea' )
			or_show_textarea( $meta, $value );
		elseif ( $meta['type'] == 'checkbox' )
			or_show_checkbox( $meta, $value );
		elseif ( $meta['type'] == 'select' )
			or_show_select( $meta, $value );
		elseif ( $meta['type'] == 'url' )
			or_show_url( $meta, $value );
	endforeach; ?>
	</table>
<?php
}
function or_create_down_box() {
	add_meta_box( 'ordown-post-meta-boxes','开启下载（请根据实际开启其中一种下载模式）', 'or_post_down_info', 'post', 'normal', 'high' );
}
function or_down_post_boxes() {  //资源名称、资源大小、更新时间、适用版本、作者信息
	$meta_boxes = array(
/*	array(
    "name"             => "ordown_start",
    "title"            => "启用下载+演示",
    "desc"             => "启用下载",
    "type"             => "checkbox",
    "capability"       => "manage_options"
    ),*/
	array(
    "name"             => "ordown_official",
    "title"            => "启用官方下载",
    "desc"             => "启用极速下载",
    "type"             => "checkbox",
    "capability"       => "manage_options"
    ),
      array(
    "name"             => "ordown_hidden",
    "title"            => "是否临时隐藏下载地址",
    "desc"             => "是否临时隐藏下载地址",
    "type"             => "checkbox",
    "capability"       => "manage_options"
    ),
    array(
    "name"             => "ordown_forever_hidden",
    "title"            => "是否永久隐藏下载地址",
    "desc"             => "是否永久隐藏下载地址",
    "type"             => "checkbox",
    "capability"       => "manage_options"
    ),
	array(
    "name"             => "ordown_xxurl_channel",
    "title"            => "XXUrl大类",
    "desc"             => "XXUrl大类",
    "type"             => "select",
    "onchange"         => "onChangeChannel(this)",
    "capability"       => "manage_options"
    ),
    array(
    "name"             => "ordown_xxurl_type",
    "title"            => "XXUrl小类",
    "desc"             => "XXUrl小类",
    "type"             => "select",
    "capability"       => "manage_options"
    ),
/*	array(
    "name"             => "ordown_speed",
    "title"            => "启用极速下载",
    "desc"             => "启用极速下载",
    "type"             => "checkbox",
    "capability"       => "manage_options"
    ),
	array(
    "name"             => "ordown_reply",
    "title"            => "启用评论下载",
    "desc"             => "启用评论下载",
    "type"             => "checkbox",
    "capability"       => "manage_options"
    ),*/
	array(
			"name"             => "ordown_name",
			"title"            => "资源名称",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
/*	array(
			"name"             => "ordown_size",
			"title"            => "资源大小",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_date",
			"title"            => "更新时间",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_version",
			"title"            => "适用版本",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_author",
			"title"            => "作者信息",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_downurl1",
			"title"            => "百度云盘",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_keyl1",
			"title"            => "秘钥 1",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_downurl2",
			"title"            => "360云盘",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_keyl2",
			"title"            => "秘钥 2",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),*/
	array(
			"name"             => "ordown_downname1",
			"title"            => "城通网盘",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_downurl4",
			"title"            => "城通网盘下载地址",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
/*	array(
			"name"             => "ordown_downname2",
			"title"            => "自定义网盘 2",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_downurl5",
			"title"            => "下载地址 2",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	),
	array(
			"name"             => "ordown_downurl3",
			"title"            => "在线演示",
			"desc"             => "",
			"type"             => "text",
			"capability"       => "manage_options"
	)*/
	);
	return apply_filters( 'ordown_post_boxes', $meta_boxes );
}
function or_post_down_info() {
	global $post;
	$meta_boxes = or_down_post_boxes(); 
?>
	<table class="form-table">
	<?php foreach ( $meta_boxes as $meta ) :
		$value = get_post_meta( $post->ID, $meta['name'], true );
		if ( $meta['type'] == 'text' )
			or_show_text_input( $meta, $value );
		elseif ( $meta['type'] == 'textarea' )
			or_show_textarea( $meta, $value );
		elseif ( $meta['type'] == 'checkbox' )
			or_show_checkbox( $meta, $value );
		elseif ( $meta['type'] == 'select' )
			or_show_select( $meta, $value );
		elseif ( $meta['type'] == 'url' )
			or_show_url( $meta, $value );
	endforeach; ?>
	</table>
<?php
}
function or_show_text_input( $args = array(), $value = false ) {
	extract( $args ); ?>
	<tr>
		<th style="width:15%;">
			<label for="<?php echo $name; ?>"><?php echo $title; ?></label>
		</th>
		<td>
		<input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo wp_specialchars( $value, 1 ); ?>" size="30" tabindex="30" style="width: 97%;" />
			<input type="hidden" name="<?php echo $name; ?>_input_name" id="<?php echo $name; ?>_input_name" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
		</td>
	</tr>
	<?php
}
function or_show_textarea( $args = array(), $value = false ) {
	extract( $args ); ?>
	<tr>
		<th style="width:10%;">
			<label for="<?php echo $name; ?>"><?php echo $title; ?></label>
		</th>
		<td>
			<textarea name="<?php echo $name; ?>" id="<?php echo $name; ?>" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars( $value, 1 ); ?></textarea>
			<input type="hidden" name="<?php echo $name; ?>_input_name" id="<?php echo $name; ?>_input_name" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />	</td>
	</tr>
	<?php
}
function or_show_checkbox( $args = array(), $value = false ) {
	extract( $args ); ?>
<tr>
		<th style="width:10%;">
	<label for="<?php echo $name; ?>"><?php echo $title; ?></label>		</th>
		<td>
    <input type="checkbox" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="yes"
    <?php if ( htmlentities( $value, 1 ) == 'yes' ) echo ' checked="checked"'; ?>
    style="width: auto;" />
    <input type="hidden" name="<?php echo $name; ?>_input_name" id="<?php echo $name; ?>_input_name" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </td>
	</tr>
	<?php
}

function or_show_select( $args = array(), $value = false ) {
	extract( $args ); ?>
<tr>
		<th style="width:10%;">
	<label for="<?php echo $name; ?>"><?php echo $title; ?></label>		</th>
		<td>
    <select name="<?php echo $name; ?>" id="<?php echo $name; ?>" style="width: auto;"  <?php if ( $onchange  !== null ){ echo 'onchange="'.$onchange.'"'; }?>  >
    		<option value="0">请选择</option>
  	<?php 
  		$str = '';
  		global $post;
  		global $channelMap;
  		global $typeMap;
  		$chanelId = get_post_meta( $post->ID, 'ordown_xxurl_channel', true );
  	if ($name  == 'ordown_xxurl_channel'){
  		for($x=0;$x<count($channelMap);$x++) {
  		  $arr = $channelMap[$x];
  		  $code = $arr['code'];
  		  $codeName = $arr['name'];
		  $str = $str.'<option value="'.$code.'" '.($chanelId == $code ? 'selected' : '').'>'.$codeName.'</option>';
		}
		echo $str;
	}else if($name == 'ordown_xxurl_type' && $chanelId !== ''){
		$channelTypeMap = $typeMap[$chanelId];
		$typeId = get_post_meta( $post->ID, $name, true );
	  	for($x=0;$x<count($channelTypeMap);$x++) {
  		  $arr = $channelTypeMap[$x];
  		  $code = $arr['code'];
  		  $codeName = $arr['name'];
		  $str = $str.'<option value="'.$code.'" '.($typeId == $code ? 'selected' : '').'>'.$codeName.'</option>';
		}
		echo $str;
	}
	?>
	</select>
    <input type="hidden" name="<?php echo $name; ?>_input_name" id="<?php echo $name; ?>_input_name" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </td>
	</tr>
<?php 
}
function or_show_url( $args = array(), $value = false ) {
	extract( $args );
	$xxurl_uid = get_option('xxurl_uid');
	$url = wp_specialchars( $value, 1 );
	$url = str_replace('${uid}', $xxurl_uid, $url);
?>
	<tr>
		<th style="width:15%;">
			<label for="<?php echo $name; ?>"><?php echo $title; ?></label>
		</th>
		<td>
		<a target="_blank" href="<?php echo $url; ?>" size="30" tabindex="30" style="width: 97%;"><?php echo $url; ?></a>
		</td>
	</tr>
	<?php
}
function or_save_down_data( $post_id ) {
		$meta_boxes = array_merge( or_down_post_boxes() );
		foreach ( $meta_boxes as $meta_box ) :
		if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_input_name'], plugin_basename( __FILE__ ) ) )
			return $post_id;
		if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) )
			return $post_id;
		elseif ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
			return $post_id;
		$data = stripslashes( $_POST[$meta_box['name']] );
		if ( get_post_meta( $post_id, $meta_box['name'] ) == '' )
			add_post_meta( $post_id, $meta_box['name'], $data, true );
		elseif ( $data != get_post_meta( $post_id, $meta_box['name'], true ) )
			update_post_meta( $post_id, $meta_box['name'], $data );
		elseif ( $data == '' )
			delete_post_meta( $post_id, $meta_box['name'], get_post_meta( $post_id, $meta_box['name'], true ) );
	endforeach;
}
?>